package com.yikaiye.android.yikaiye.ui.activity.mine;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityNicknameBinding;
import com.yikaiye.android.yikaiye.model.EventBusModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel.UserInfoViewModel;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

public class NicknameActivity extends BaseActivity<ActivityNicknameBinding, UserInfoViewModel> {

    @Override
    protected UserInfoViewModel bindModel() {
        return getViewModel(this, UserInfoViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void initData() {
        initTitleBar(getString(R.string.real_name));
        if (null != getIntent().getExtras()) {
            String userName = getIntent().getExtras().getString(Constant.USER_NAME_KEY);
            binding.etContent.setText(userName);
        }
        if (!TextUtils.isEmpty(binding.etContent.getText().toString().trim())) {
            binding.tvSave.setEnabled(true);
            binding.tvSave.setBackgroundResource(R.drawable.shape_f93f50_4dp);
        } else {
            binding.tvSave.setEnabled(false);
            binding.tvSave.setBackgroundResource(R.drawable.shape_d7d7d7_4dp);
        }
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.tvSave, o -> {
            showLoadingDialog();
            Map<String, Object> map = new HashMap<>();
            map.put("userName", binding.etContent.getText().toString().trim());
            map.put("userDetailId", MyApplication.manager.getUserDetailId());
            viewModel.updateUserInfo(map);
        });
        binding.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(binding.etContent.getText().toString().trim())) {
                    binding.tvSave.setEnabled(true);
                    binding.tvSave.setBackgroundResource(R.drawable.shape_f93f50_4dp);
                } else {
                    binding.tvSave.setEnabled(false);
                    binding.tvSave.setBackgroundResource(R.drawable.shape_d7d7d7_4dp);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        viewModel.changeSuccessMutableLiveData.observe(this, s -> {
            ToastHelper.show(getString(R.string.change_successfully));
            EventBus.getDefault().post(new EventBusModel(Constant.refreshUserInfo));
            Intent intent = new Intent();
            intent.putExtra(Constant.USER_NAME_KEY, binding.etContent.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
        });

        viewModel.changeErrorMutableLiveData.observe(this, s -> {
            ToastHelper.show(s);
            dismissLoadingDialog();
        });
    }
}